home *** CD-ROM | disk | FTP | other *** search
/ AP Professional Graphics CD-ROM Library / AP Professional Graphics CD-ROM Library.iso / pc / pc / appendix / gemsiii / ndline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  2.5 KB  |  86 lines

  1. /* Badouel / Wuthrich "Face Connected Line Segment Generation in an 
  2.    N-dimensional Space" 
  3. */
  4. /******************************************************************************
  5. Data structure for n-dimensional line segment generation. Initialized by the 
  6. procedure Init() and used by the procedure Incr().
  7. ******************************************************************************/
  8.  
  9. #define DIM   4                  /* number of dimensions                     */
  10.  
  11. typedef struct {
  12.   int  D[DIM];                   /* counter for each of the N dimensions     */
  13.   int  N[DIM];                   /* increment for each of the N dimensions   */
  14.   int  S[DIM];                   /* orientation for each of the N dimensions */
  15.   int  cm;                       /* common multiple                          */
  16. } Nline;
  17.  
  18. /******************************************************************************
  19. Init(): initializes the data structure in order to generate the discrete
  20. path between the point P and the point Q in an n-dimensional space.
  21.  
  22. This procedure should be called once before using Incr().
  23.  
  24. Entry:    P    - origin point
  25.         Q    - destination point
  26.         line - line segment data structure
  27. ******************************************************************************/
  28.  
  29. void Init (P, Q, line)
  30.      int   *P, *Q;
  31.      Nline *line;
  32. {
  33.   int i, v;
  34.  
  35.   line->cm = 1;
  36.   for (i=0; i<DIM; i++) {
  37.     v = Q[i] - P[i];
  38.     if (v < 0) {
  39.       line->S[i] = -1; line->N[i] = -v;
  40.     } else {
  41.       line->S[i] =  1; line->N[i] =  v;
  42.     }
  43.     if (line->N[i] != 0) line->cm *= line->N[i];
  44.   }
  45.   for (i=0; i<DIM; i++) {
  46.     if (line->N[i] == 0) 
  47.       line->D[i] = 2*line->cm;
  48.     else {
  49.       line->D[i] = line->cm/line->N[i];
  50.       line->N[i] = 2*line->D[i];
  51.     }
  52.   }
  53. }
  54.  
  55. /******************************************************************************
  56. Incr(): generates one step of a discrete segment line in an n-dimensional 
  57. space. Indicate the end of the generation with the returned value -1 for 
  58. the direction.
  59.  
  60. The procedure Init() must be called once before using Incr(). 
  61.  
  62. Entry:    line - line segment data structure
  63. Exit:   d    - current step direction
  64.         s    - current step orientation
  65. ******************************************************************************/
  66.  
  67. int Incr (line, d, s)
  68.      Nline *line;
  69.      int   *d, *s;
  70. {
  71.   int i, v = 2*line->cm;
  72.  
  73.   *d = -1;
  74.   for (i=0; i<DIM; i++) {
  75.     if (line->D[i] < v) {
  76.       v = line->D[i];
  77.       *d = i;
  78.     }
  79.   }
  80.   line->D[*d] += line->N[*d];
  81.   *s = line->S[*d];
  82.   return *d;
  83. }
  84.  
  85.  
  86.